package com.cancer.common.util;

import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class I18 {
    final static Logger logger = LoggerFactory.getLogger(I18.class);
    final static Map<String, ResourceBundle> CACHED_BUNDLES = new HashMap<>();
    final static GroupTemplate strGt = getGroupTemplate();

    private static GroupTemplate getGroupTemplate() {
        try {
            StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
            Configuration cfg = Configuration.defaultConfiguration();
            GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, cfg);
            return groupTemplate;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String msg(String baseName, Locale locale, EnumTplOrMsgType type, String key, Object... objs) {
        if (locale == null) {
            locale = Locale.CHINA;
        }
        String str = "";
        String cacheKey = baseName + locale;
        ResourceBundle resourceBundle = CACHED_BUNDLES.get(cacheKey);
        if (resourceBundle == null) {
            synchronized (CACHED_BUNDLES) {
                resourceBundle = ResourceBundle.getBundle(baseName, locale);
                CACHED_BUNDLES.put(cacheKey, resourceBundle);
            }
        }

        String keyVal ="";
        try {
            keyVal=resourceBundle.getString(key);
            if (StringUtils.isBlank(keyVal)) {
                logger.info("国际化key：[{}] 没有配置",key);
            }else{
                if (objs != null && objs.length > 0) {
                    //传递了参数 ，根据类型（java标准和）
                    if (EnumTplOrMsgType.BEETLE_MSG.equals(type)) {
                        Template t = strGt.getTemplate(keyVal);
                        HashMap map = new HashMap(1);
                        map.put("p", objs);
                        t.binding(map);
                        str = t.render();
                    } else {
                        str = MessageFormat.format(keyVal, objs);
                    }
                } else {
                    str = keyVal;
                }
            }

        } catch (Exception e) {
            logger.error("国际化key：[{}] 没有配置",key);
        }
        return str;
    }

    public enum EnumTplOrMsgType {
        BEETLE_MSG(0, "beetle模板类型的国际化"),
        MSG(1, "标准java国际化");

        private EnumTplOrMsgType(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        private int value;
        private String desc;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
